/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab7review;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;

/**
 *
 * @author THIEN
 */
public class StudentManagement {

    Student student;
    ArrayList<Student> list = new ArrayList<Student>();
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(reader);

    public void inputData() throws IOException {
        for (int i = 0; i < 2; i++) {
            System.out.println("Roll number: ");
            String rollNumber = in.readLine();

            System.out.println("Name: ");
            String name = in.readLine();

            System.out.println("Address: ");
            String address = in.readLine();

            System.out.println("Age: ");
            int age = Integer.parseInt(in.readLine());

            student = new Student(rollNumber, name, address, age);
            list.add(student);
        }
    }

    public void displayData() {
        for (Student student : list) {
            System.out.println(student);

        }
    }

    public void save2File() throws FileNotFoundException, IOException {

        /**
         * write by object type
         */
        FileOutputStream f = new FileOutputStream("list.txt", true);
        ObjectOutputStream writer = new ObjectOutputStream(f);
        writer.writeObject(list);
        writer.flush();
        writer.close();
        f.close();
    }
    public void readFromFile() throws FileNotFoundException, IOException, ClassNotFoundException{
        FileReader fReader = new FileReader("list.txt");
        BufferedReader bReader = new BufferedReader(fReader);
        //FileInputStream f1 = new FileInputStream("list.txt");
        //ObjectInputStream reader = new ObjectInputStream(f1);
        //Object list = reader.readObject();
        ArrayList<Student> StudentList = (ArrayList<Student>) list;
        for (Student student : StudentList) {
            System.out.println(student);

        }
    }
}
